﻿function ProjectAttachment(parentContainer, countContainer, templateContainer, newIDPrefix, removeIDPrefix, noPrefix, ID, userID, tableID) {
    this.ParentContainer = parentContainer;
    this.CountContainer = countContainer;
    this.TemplateContainer = templateContainer;
    this.TemplateContainerHtmlContent = templateContainer.innerHTML;
    this.Items = new Array();
    this.NewID = newIDPrefix;
    this.RemoveID = removeIDPrefix;
    this.NoPrefix = noPrefix;
    this.ID = ID;
    this.UserID = userID;
    this.TableID = tableID;
}

ProjectAttachment.prototype.addMember = function (fileName,fileType) {
    if (this.Items.contains(fileName)) {
        return false;
    }
    var count = parseInt(this.CountContainer.value) + 1;
    this.CountContainer.value = count;

    var templateContent = new String(this.TemplateContainerHtmlContent);
    templateContent = templateContent.replace(/_0/gi, "_" + count);
    templateContent = templateContent.replace(/_count/gi, count);
    templateContent = templateContent.replace(/_TableID/gi, this.TableID);
    templateContent = templateContent.replace(/_ID/gi, this.ID);
    templateContent = templateContent.replace(/_FileName/gi, fileName);
    templateContent = templateContent.replace(/_FileType/gi, fileType);
    var newContainer = document.createElement("DIV");
    newContainer.id = this.NewID + count;
    newContainer.innerHTML = templateContent;
    this.ParentContainer.appendChild(newContainer);

    setRemoveControl(document.getElementById(this.NewID + count), true);

    this.Items.push(fileName);
    reOrder(parseInt(this.CountContainer.value), this.ParentContainer, this.NewID, this.NoPrefix);

    return true;
}

ProjectAttachment.prototype.removeMember = function (no, fileName) {
    var allContainers = this.ParentContainer.getElementsByTagName("DIV");
    for (var i = 0; i < allContainers.length; i++) {
        if (allContainers[i].id == this.NewID + no) {
            this.ParentContainer.removeChild(allContainers[i]);
            this.Items.remove(fileName);
            break;
        }
    }
    var count = parseInt(this.CountContainer.value);
    count--;
    this.CountContainer.value = count;
    reOrder(parseInt(this.CountContainer.value), this.ParentContainer, this.NewID, this.NoPrefix);

    var postData =
    {
        "TableID": this.TableID,
        "ID": encodeURIComponent(this.ID),
        "FileName": encodeURIComponent(fileName),
        "LastModifiedBy": encodeURIComponent(this.UserID)
    };
    var jsonStr = $.toJSON(postData);

    $.ajax({
        url: "/Handler/Attachment.ashx?" + (new Date()).valueOf(),
        contentType: 'application/x-www-form-urlencoded;charset=gb2312',
        data: { action: "removeData", jsonDataString: jsonStr },
        success: function (data) {

        }
    });
}

function reOrder(count, parentContainer, newID, noPrefix) {
    var allContainers = parentContainer.getElementsByTagName("DIV");
    var cnt = 1;
    for (var i = 0; i < allContainers.length; i++) {
        var id = allContainers[i].id;
        var index = id.toString().indexOf(newID) + newID.length;
        var noID = noPrefix + id.substring(index);
        document.getElementById(noID).value = cnt;
        cnt++;
    }
}

function setRemoveControl(parentContainer, isShow) {
    var removeCtrls = parentContainer.getElementsByTagName("a");
    for (var i = 0; i < removeCtrls.length; i++) {
        if (removeCtrls[i].id.indexOf(this.RemoveID) >= 0) {
            removeCtrls[i].style.display = isShow ? "" : "none";
        }
    }
} 